package com.hondee.fuzzyquery.utils;

import java.util.Collection;
import java.util.Iterator;

public class StringUtil {
	public static boolean isNullOrEmpty(Object obj) {	
		return obj == null || "".equals(obj.toString());
	}
	public static String toString(Object obj){
		if(obj == null) return "null";
		return obj.toString();
	}
	public static String join(Collection<?> s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator<?> iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }
	public static String combine(Object[] datas,String symbol){
		StringBuffer str = new StringBuffer();
		if(datas!=null){
			for(Object row:datas){
				str.append(row).append(symbol);
			}
		}
		if(str.length()>0){
			return str.substring(0,str.length()-1);
		}
		else{
			return "";
		}
	}
	
	/**
	 * If target string is null or empty return default string, or return the target string.
	 * @param targetStr
	 * @param defaultStr
	 * @return
	 * @author Allen
	 * @date 2015年3月19日
	 */
	public static String defaultIfNullOrEmpty(String targetStr,String defaultStr){
		return isNullOrEmpty(targetStr)?defaultStr:targetStr;
	}
	
}